using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.Unity;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.Collections;
using ScrumTable.UI.View.Extension;
using ScrumTable.UI.View.Localization;
using ScrumTable.UI.ViewModel;
using ScrumTable.UI.ViewModel.View;

namespace ScrumTable.UI.View.WinTouch.UserControls.View
{
    /// <summary>
    /// Interaction logic for ProjectPlanningUserStories.xaml
    /// </summary>
    public partial class ProjectPlanningUserStories
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------


        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets or sets the container.
        /// </summary>
        /// <value>The container.</value>
        [Dependency]
        public IUnityContainer Container { get; set; }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        [Dependency]
        public ProjectPlanningUserStoriesViewModel Model
        {
            set
            {
                ViewModel = value;
            }
            get
            {
                return (ProjectPlanningUserStoriesViewModel)ViewModel;
            }
        }
        
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------	
        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectPlanningUserStories"/> class.
        /// </summary>
        public ProjectPlanningUserStories()
        {
            InitializeComponent();

            UserStorieListMust.CanDragDrop = (obj) => DropUserControlExtension.ConvertBoolToEnum(obj is UserStoryViewModel || obj is BugViewModel);
            UserStorieListCould.CanDragDrop = (obj) => DropUserControlExtension.ConvertBoolToEnum(obj is UserStoryViewModel || obj is BugViewModel);
            UserStorieListShould.CanDragDrop = (obj) => DropUserControlExtension.ConvertBoolToEnum(obj is UserStoryViewModel || obj is BugViewModel);
            UserStorieListNone.CanDragDrop = (obj) => DropUserControlExtension.ConvertBoolToEnum(obj is UserStoryViewModel || obj is BugViewModel);
        }
        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        private void SetRank(ItemsControl listBox, SurfaceDragCursor cursor, Range range)
        {
            if (!(cursor.Data is IScrumItemViewModel<DomainBase>)) return;

            IScrumItemViewModel<DomainBase> userStory = (IScrumItemViewModel<DomainBase>)cursor.Data;
            using (Model.ScrumData.DataContext.UndoManager.CaptureUndoRedoAction(LanguageDictionary.Current.Translate<string>("Undo_StackRank", "Content")))
            {
                for (int i = 0; i < listBox.Items.Count; i++)
                {
                    IScrumItemViewModel<DomainBase> targetUserStorie = GetTargetUserStory(cursor, i, listBox);

                    if (targetUserStorie != null)
                    {
                        Model.SetStackRank(userStory, targetUserStorie.Ranking.Value, range.Min, range.Max);
                        return;
                    }
                }
                userStory.Ranking = Model.GetNewStackStackInRange(range);
            }
        }


        private static IScrumItemViewModel<DomainBase> GetTargetUserStory(SurfaceDragCursor cursor, int i, ItemsControl listBox)
        {

            var listBoxItem = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(i));
            if (listBoxItem == null || !listBoxItem.IsVisible)
            {
                return null;
            }
            var pointItem = listBoxItem.PointToScreen(new Point(0, 0));
            pointItem = Application.Current.MainWindow.PointFromScreen(pointItem);

            var pointCursor = cursor.GetPosition(Application.Current.MainWindow);

            bool firstBool = pointCursor.X >= pointItem.X && pointCursor.X <= pointItem.X + listBoxItem.ActualWidth;
            bool secondBool = pointCursor.Y >= pointItem.Y &&
                              pointCursor.Y <= pointItem.Y + listBoxItem.ActualHeight;
            IScrumItemViewModel<DomainBase> targetUserStorie = null;

            if (firstBool && secondBool)
            {
                targetUserStorie = ((IScrumItemViewModel<DomainBase>)listBox.Items[i]);
            }
            return targetUserStorie;
        }

        protected override void ReleaseManagedResources()
        {
            base.ReleaseManagedResources();
        }
        #endregion

        #region Events

        private void OnMustDrop(object sender, SurfaceDragDropEventArgs e)
        {
            SetRank(UserStorieListMust, e.Cursor, Model.RangeMust);

        }

        private void OnShouldDrop(object sender, SurfaceDragDropEventArgs e)
        {
            SetRank(UserStorieListShould, e.Cursor, Model.RangeShould);
        }

        private void OnCouldDrop(object sender, SurfaceDragDropEventArgs e)
        {
            SetRank(UserStorieListCould, e.Cursor, Model.RangeCould);
        }

        private void OnNoneDrop(object sender, SurfaceDragDropEventArgs e)
        {
            if (!(e.Cursor.Data is IScrumItemViewModel<DomainBase>)) return;

            var rankingItem = (IScrumItemViewModel<DomainBase>)e.Cursor.Data;
            rankingItem.Ranking = null;
        }

        private void OnAddStoryClick(object sender, RoutedEventArgs e)
        {
            var story = Model.ScrumData.CurrentProject.Stories.AddNew();
            var storyControlBack = new UserStoryControlBack() { DataContext = new UserStoryViewModel(story) };
            ((AppScreen)Application.Current.MainWindow).AddScatteredItem(storyControlBack, ((Button)sender).PointToScreen(new Point(0, 0)), true);
        }


        #endregion

    }

   
}
